package com.fuxian.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DateUtile {
	private static final Log log = LogFactory.getLog(DateUtile.class);

	public static String timeStampToString(Timestamp timestamp) {
		return timeStampToString(timestamp, "yyyy-MM-dd HH:mm:ss");
	}

	public static String timeStampToString(Timestamp timestamp, String formatStr) {
		SimpleDateFormat df = new SimpleDateFormat(formatStr);
		return df.format(timestamp);
	}

	public static Timestamp stringToTimestamp(String dateStr) {
		return stringToTimestamp(dateStr, "yyyy-MM-dd HH:mm:ss");
	}

	public static Timestamp stringToTimestamp(String dateStr, String formatStr) {
		SimpleDateFormat df = new SimpleDateFormat(formatStr);

		Timestamp ts = null;
		try {
			ts = new Timestamp(df.parse(dateStr).getTime());
		} catch (ParseException e) {
			log.error(e.getStackTrace(), e);
		}
		return ts;
	}

	public static Timestamp getTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}
}
